package com.xcity.robot.net.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xcity.pomelo.request.PlayerLoadRequest;
import com.xcity.robot.net.Session;
import com.xcity.robot.net.packet.BinaryPacket;
import com.xcity.robot.net.packet.PacketHandler;
import com.xcity.robot.net.util.SessionUtils;

import naga.x.net.Packet;
import naga.x.util.CommonUtils;

public class LoginResponseHandler implements PacketHandler {

	public static final LoginResponseHandler DEFAULT = new LoginResponseHandler();
	
	@Override
	public void handle(Session session, Packet packet) {
//		JSONObject json = packet.getData();
		if (!(packet instanceof BinaryPacket)) {
			return;
		}
		JSONObject json = (JSONObject) JSON.parse(((BinaryPacket) packet).toBinary());
		json = json.getJSONObject("user");
		if (json != null) {
			String uid = json.getString("userId");
			String uname = json.getString("userName");
			
			PlayerLoadRequest req = new PlayerLoadRequest();
			req.setUid(uid);
			req.setName(uname);
			req.setCity("朝阳");
			req.setProvince("北京");
			req.setHeadimgurl("http://www.ld12.com/upimg358/20160130/235139129118238.jpg");
			req.setSex((byte) (CommonUtils.randomBoolean() ? 1 : 2));
			req.setSerial(SessionUtils.nextSerial(session));
			
			session.write(req, PlayerLoadResponseHandler.DEFAULT);
		}
	}

}
